package direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import constant.Constant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProductDirect {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPort(Constant.PORT);
        connectionFactory.setVirtualHost(Constant.VIRTUAL_HOST);
        connectionFactory.setUsername(Constant.USER_NAME);
        connectionFactory.setPassword(Constant.PASSWORD);
        connectionFactory.setHost(Constant.HOST);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare(Constant.DIRECT_EXCHANGE_NAME, BuiltinExchangeType.DIRECT,true,false,false,null);
        //声明队列
        channel.queueDeclare(Constant.DIRECT_QUEUE_NAME_1,true,false,false,null);
        channel.queueDeclare(Constant.DIRECT_QUEUE_NAME_2,true,false,false,null);
        //绑定交换机和队列
        channel.queueBind(Constant.DIRECT_QUEUE_NAME_1,Constant.DIRECT_EXCHANGE_NAME,"a");
        channel.queueBind(Constant.DIRECT_QUEUE_NAME_2,Constant.DIRECT_EXCHANGE_NAME,"b");
        channel.queueBind(Constant.DIRECT_QUEUE_NAME_2,Constant.DIRECT_EXCHANGE_NAME,"c");
        String msg1="direct,a";
        String msg2="direct,b";
        String msg3="direct,c";
        channel.basicPublish(Constant.DIRECT_EXCHANGE_NAME,"a",null,msg1.getBytes());
        channel.basicPublish(Constant.DIRECT_EXCHANGE_NAME,"b",null,msg2.getBytes());
        channel.basicPublish(Constant.DIRECT_EXCHANGE_NAME,"c",null,msg3.getBytes());
        channel.close();
        connection.close();


    }
}
